package com.householditemtwo.admin.dao;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.householditemtwo.common.entity.News;
import com.householditemtwo.common.entity.Newstype;
import com.householditemtwo.common.utils.DBManager;
import com.householditemtwo.common.utils.QueryDataResult;

/**
 * 新闻DAO
 * @author admin
 *
 */
	public class NewsTypeDao {
			
		/**
		 * 查询新闻的总条数(分页)
		 * @return
		 */
			public int count(String newstypename) {
				DBManager db=new DBManager();
				String sql="select count(*) from newstype ";
				if (newstypename !=null && newstypename !="" ) {
					sql+=" where newstypename like '%"+newstypename+"%'";
				}
				QueryDataResult qdr=db.executeQuery(sql);
			int total=0;
				try {
					while(qdr.rs.next()) {
					total=qdr.rs.getInt(1);
					}
				}catch(SQLException e) {
					e.printStackTrace();
				}
				//返回查询的结果
				return total;
			}
			
		/**
		 * 查询所有新闻
		 * @return
		 */
			public List<Newstype> selectAllNews( int page, int pageSize,String newstypename) {
				DBManager db=new DBManager();
				String sql="select * from newstype ";
						
					if (newstypename !=null && newstypename !="" ) {
							sql  +=" where newstypename like '%"+newstypename+"%'";
					}
					sql+= " order by newstypeid limit "+pageSize*(page-1)+","+pageSize;
				QueryDataResult qdr=db.executeQuery(sql);
				ArrayList<Newstype> list=new ArrayList<Newstype>();
				try {
					while(qdr.rs.next()) {
						Newstype c=new Newstype();
						c.setNewstypeid(qdr.rs.getInt(1));
						c.setNewstypename(qdr.rs.getString(2));
						list.add(c);
					}
				}catch(SQLException e) {
					e.printStackTrace();
				}
				//返回查询的结果
				return list;
			}
			
			
			/**
			 * 修改新闻
			 * @return
			 */
		
		public int update(Newstype p) {
				//写sql
			String sql= String.format("update newstype set newstypename='%s'  where newstypeid=%d",
					p.getNewstypename(),p.getNewstypeid());
			System.out.println(sql);
				//执行sql
				return	DBManager.executeUpdate(sql);
			
			
		}
		/**
		 * 新增新闻
		 * @return
		 */

		public int insert(Newstype p) {
			//拼接sql
			String sql= String.format("insert into newstype(newstypename)"
					+ " value('%s') ",p.getNewstypename());
					 System.out.println(sql);
			//执行sql
		return	DBManager.executeUpdate(sql);
			
			
		}
	
		/**
		 * 删除新闻
		 * @return
		 */
			public int delete(int id) {
				//拼接sql
				String sql= "delete from newstype  where newstypeid="+id;
						
				//执行sql
			return	DBManager.executeUpdate(sql);
			}
	
}
